<template>
  <div>
<el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="100px" style="width: 500px">

<el-form-item label="真实姓名" prop="name">
  <el-input v-model="ruleForm.name"></el-input>
</el-form-item>

<el-form-item label="账号" prop="account">
  <el-input v-model="ruleForm.account"></el-input>
</el-form-item>

<el-form-item label="密码" prop="password">
  <el-input v-model="ruleForm.password"></el-input>
</el-form-item>

  <el-form-item label="联系方式" prop="connection">
    <el-input v-model="ruleForm.connection"></el-input>
  </el-form-item>

<el-form-item label="注册类型" prop="typeid">
  <el-select v-model="ruleForm.typeid" placeholder="请选择注册类型">
    <el-option label="云工厂管理员" value=1></el-option>
    <el-option label="经销商" value=2></el-option>
  </el-select>
</el-form-item>


<div v-if='ruleForm.typeid==1'>
  <el-form-item label="工厂名" prop="factoryname">
    <el-input v-model="ruleForm.factoryname"></el-input>
  </el-form-item>

  <el-form-item label="工厂简介" prop="description">
    <el-input v-model="ruleForm.description" type="textarea"></el-input>
  </el-form-item>
</div>


<el-form-item>
  <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
  <el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>

</el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        account: '',
        password: '',
        typeid:'',
        connection:'',
        factoryname:'',
        description:''
      },
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '账号不能为空', trigger: 'change' }
        ],
        typeid:[
          { required: true, message: '必须选择一个类型', trigger: 'change' }
        ],
        password:[
          { required: true, message: '密码不能为空', trigger: 'change' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        connection:[
            { required: true, message: '联系方式不能为空', trigger: 'change' }
        ],
        description: [
          {max:200,message:'最多不超过200个字符',trigger: 'change'}
        ],
        factoryname:[
          { required: true, message: '工厂名不能为空', trigger: 'change' }
        ]
      },
    };
  },created() {

  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8081/signup',this.ruleForm).then((response)=>{
            if(response.data != null){
              this.success();
              this.$router.push("/userList");
            }else{
              this.failed();
            }
          })
        } else {
          this.failed();
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    success() {
      this.$message({
        message: '添加成功!',
        type: 'success'
      });
    },
    failed() {
      this.$message.error('添加失败，请检查内容是否正确或稍后再试!');
    }
  }
}
</script>